package com.example.helloworldforpython;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.stetho.ai.b.R;
import com.stetho.ai.b.aidl.AIServiceInterface;


public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private AIServiceInterface anInterface;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            anInterface = AIServiceInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            anInterface = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text0);
        findViewById(R.id.button).setOnClickListener(v -> {
            try {
                anInterface.init();
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent("com.stetho.ai.a.ACTION_BIND_AI_SERVICE");
        intent.setPackage("com.stetho.ai.a");
        boolean isSuccess = bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
        Toast.makeText(this, "bind=" + isSuccess, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(serviceConnection);
    }
}